home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------//
-
- // Synopsis: Tridiagonal matrix
-
- // Syntax: T = tridiag ( X , Y , Z )
-
- // Description:
-
- // T is the tridiagonal matrix with subdiagonal X, diagonal Y,
- // and superdiagonal Z. X and Z must be vectors of dimension one
- // less than Y.
-
- // Alternatively TRIDIAG(N, C, D, E), where C, D, and E are all
- // scalars, yields the Toeplitz tridiagonal matrix of order N
- // with subdiagonal elements C, diagonal elements D, and
- // superdiagonal elements E.
-
- // This matrix has eigenvalues (Todd 1977)
- // D + 2*SQRT(C*E)*COS(k*PI/(N+1)), k=1:N.
- // tridiag(N) is the same as tridiag(N,-1,2,-1), which is a
- // symmetric positive definite M-matrix (the negative of the
- // second difference matrix).
-
- // References:
- // J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
- // Birkhauser, Basel, and Academic Press, New York, 1977, p. 155.
- // D.E. Rutherford, Some continuant determinants arising in physics and
- // chemistry---II, Proc. Royal Soc. Edin., 63, A (1952), pp. 232-241.
-
- // This file is a translation of tridiag.m from version 2.0 of
- // "The Test Matrix Toolbox for Matlab", described in Numerical
- // Analysis Report No. 237, December 1993, by N. J. Higham.
-
- //-------------------------------------------------------------------//
-
- tridiag = function ( n , x , y , z )
- {
- local (n, x, y, z)
-
- if (!exist (x)) // nargin = 1
- {
- x = -1; y = 2; z = -1;
- }
-
- if (!exist (z)) // nargin = 3
- {
- z = y; y = x; x = n;
- }
-
- x = x[:]; y = y[:]; z = z[:]; // Force column vectors.
-
- if (max( [ size(x), size(y), size(z) ] ) == 1)
- {
- x = x*ones(n-1,1);
- z = z*ones(n-1,1);
- y = y*ones(n,1);
- else
- nx = x.nr;
- ny = y.nr;
- nz = z.nr;
- if ((ny - nx - 1) || (ny - nz - 1)) {
- error("Dimensions of vector arguments are incorrect.")
- }
- }
-
- T = diag(x, -1) + diag(y) + diag(z, 1); // For non-sparse matrix.
-
- // n = max(size(y)); // For sparse matrix
- // T = spdiags([ [x;0] y [0;z] ], -1:1, n, n);
-
- return T;
- };
-